package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.domain.model.Sort;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.UserIntegralDAO;
import com.jinwei.qunguanjia.user.dto.UserIntegralDTO;
import com.jinwei.qunguanjia.user.po.UserIntegralPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("userIntegralService")
public class UserIntegralServiceImpl implements UserIntegralService {

    @Resource
    private UserIntegralDAO userIntegralDAO;

    @Override
    public String save(UserIntegralDTO userIntegralDTO) {
        UserIntegralPO userIntegralPO = new UserIntegralPO();
        BeanUtils.copyProperties(userIntegralDTO, userIntegralPO);
        String userIntegralId = IdUtils.getWorkerId();
        userIntegralPO.setUserIntegralId(userIntegralId);
        userIntegralPO.setCreateTime(DateUtils.getDateTime());
        userIntegralDAO.insert(userIntegralPO);
        return userIntegralId;
    }

    @Override
    public void remove(String id) {
        userIntegralDAO.deleteById(id);
    }

    @Override
    public void update(UserIntegralDTO userIntegralDTO) {
        UserIntegralPO userIntegralPO = new UserIntegralPO();
        BeanUtils.copyProperties(userIntegralDTO, userIntegralPO);
        userIntegralPO.setUpdateTime(DateUtils.getDateTime());
        userIntegralDAO.updateById(userIntegralPO);
    }

    @Override
    public UserIntegralDTO get(String id) {
        UserIntegralPO userIntegralPO = userIntegralDAO.selectById(id);
        if (null == userIntegralPO) {
            return null;
        }
        UserIntegralDTO userIntegralDTO = new UserIntegralDTO();
        BeanUtils.copyProperties(userIntegralPO, userIntegralDTO);
        return userIntegralDTO;
    }

    @Override
    public UserIntegralDTO get(UserIntegralDTO userIntegralDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userIntegralDTO);
        // 拼接查询条件
        QueryWrapper<UserIntegralPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        UserIntegralPO userIntegralPO = userIntegralDAO.selectOne(queryWrapper);
        if (null == userIntegralPO) {
            return null;
        }
        BeanUtils.copyProperties(userIntegralPO, userIntegralDTO);
        return userIntegralDTO;
    }

    @Override
    public Long count(UserIntegralDTO userIntegralDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userIntegralDTO);
        // 拼接查询条件
        QueryWrapper<UserIntegralPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return userIntegralDAO.selectCount(queryWrapper);
    }

    @Override
    public List<UserIntegralDTO> list(UserIntegralDTO userIntegralDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userIntegralDTO);
        // 拼接查询条件
        QueryWrapper<UserIntegralPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(userIntegralDAO.selectList(queryWrapper), UserIntegralDTO.class);
    }

    @Override
    public PageDataInfo<UserIntegralDTO> listPage(UserIntegralDTO userIntegralDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userIntegralDTO);
        // 拼接查询条件
        QueryWrapper<UserIntegralPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time",Sort.DESC));
        SearchUtils.parseOrderBySql(queryWrapper, sortList);
        List<UserIntegralPO> list = userIntegralDAO.selectList(queryWrapper);
        PageInfo<UserIntegralPO> pageInfo = new PageInfo<>(list);
        PageDataInfo<UserIntegralDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, UserIntegralDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(UserIntegralDTO userIntegralDTO) throws IllegalAccessException {
        String userIntegralId = StringUtils.isNull(userIntegralDTO.getUserIntegralId()) ? "-1" : userIntegralDTO.getUserIntegralId();
        UserIntegralDTO info = get(userIntegralDTO);
        if (StringUtils.isNotNull(info) && !info.getUserIntegralId().equals(userIntegralId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
